package com.example.springboot.controller;

import com.example.springboot.entity.ForumPost;
import com.example.springboot.service.ForumPostService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/admin/posts")
public class ForumPostAdminController {

    @Autowired
    private ForumPostService forumPostService;

    @GetMapping
    public ResponseEntity<Page<ForumPost>> getAllPosts(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String query,
            HttpServletRequest request) {
        String username = (String) request.getAttribute("username");
        Page<ForumPost> posts = forumPostService.getAllPostsForAdmin(username, page, size, query);
        if (posts == null) {
            return ResponseEntity.status(403).body(null);
        }
        return ResponseEntity.ok(posts); // 直接返回 Page 对象
    }

    @DeleteMapping("/{postId}")
    public ResponseEntity<Void> deletePost(
            @PathVariable Integer postId,
            HttpServletRequest request) {
        String username = (String) request.getAttribute("username");
        boolean success = forumPostService.deletePostAdmin(postId, username);
        if (!success) {
            return ResponseEntity.status(403).build();
        }
        return ResponseEntity.ok().build();
    }
}